package com.hzjiteng.newsbymvp.news;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.bean.NewsBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * @author Administrator
 * @Email:lijiayan_mail@163.com
 * @created_time 2016/07/29 17:56
 */
public class NewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final static int TYPE_ITEM = 0;

    private final static int TYPE_FOOTER = 1;

    private List<NewsBean> list;

    private boolean showFooter = true;

    private Context context;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public NewsListAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<NewsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (!showFooter)
            return TYPE_ITEM;
        if (position + 1 == getItemCount())
            return TYPE_FOOTER;
        else
            return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.newslist_item, parent, false);
            return new ViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.footer, parent, false);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new FooterViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            NewsBean bean = list.get(position);
            if (bean == null)
                return;
            ImageLoader.getInstance().displayImage(bean.getImgsrc(),  ((ViewHolder) holder).ivNews);
            ((ViewHolder) holder).tvTitle.setText(bean.getTitle());
            ((ViewHolder) holder).tvDesc.setText(bean.getDigest());
        }
    }


    @Override
    public int getItemCount() {
        int bean = showFooter?1:0;
        if(list==null)
            return bean;
        return list.size()+bean;
    }


    public NewsBean getItemBean(int position){
        return list == null ? null : list.get(position);
    }

    public boolean isShowFooter() {
        return showFooter;
    }

    public void setShowFooter(boolean showFooter) {
        this.showFooter = showFooter;
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivNews;
        private TextView tvTitle;
        private TextView tvDesc;

        public ViewHolder(View itemView) {
            super(itemView);
            ivNews = (ImageView) itemView.findViewById(R.id.ivNews);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvDesc = (TextView) itemView.findViewById(R.id.tvDesc);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(v, this.getLayoutPosition());
            }
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }
}
